package com.exmaple.cdc.lock;


import javax.sql.DataSource;
import java.util.logging.Logger;

/***
 * 获取并执行
 */
public class MySqlLockUtils {

    static Logger logger = Logger.getLogger(MySqlLockUtils.class.getName());

    public static void inLockAction(DataSource dataSource, SyncCode syncCode) {
        MySqlLock mySqlLock = null;
        try {
            mySqlLock = new MySqlLock(dataSource);
            mySqlLock.lock();
            syncCode.sync();
        } catch (Exception e) {
            logger.warning("inLockAction error : " + e.getMessage());
        } finally {
            if (mySqlLock != null) {
                mySqlLock.unlock();
            }
        }
    }

    public interface SyncCode {

        void sync();
    }

    public static void main(String[] args) {
        DataSource dataSource = null;
        MySqlLockUtils.inLockAction(dataSource, new SyncCode() {
            @Override
            public void sync() {

            }
        });
    }
}
